import { createStore, applyMiddleware } from 'redux'
import { composeWithDevTools } from 'redux-devtools-extension'
import { logger } from 'redux-logger'
import thunkMiddleware from 'redux-thunk'
import createSagaMiddleware from 'redux-saga'
import rootSaga from './sagas'
import reducer from './reducers/index'

const sagaMiddleware = createSagaMiddleware() // 执行

// 如果是在客户端环境，并且是开发模式，那么打印redux日志
const bindMiddleware = middleware => {
    if (process.env.NODE_ENV !== 'production') {
        middleware.push(logger)
        return composeWithDevTools(applyMiddleware(...middleware))
    }
    return applyMiddleware(...middleware)
}

const store = createStore(
    reducer,
    //   composeWithDevTools(applyMiddleware(sagaMiddleware))
    bindMiddleware([thunkMiddleware, sagaMiddleware])
)
sagaMiddleware.run(rootSaga)
export default store

/*
import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import { logger } from 'redux-logger';
import thunkMiddleware from 'redux-thunk';
import createSagaMiddleware from 'redux-saga';
import rootSaga from './sagas';
import reducers from './reducers/index';

// 如果是在客户端环境，并且是开发模式，那么打印redux日志
const bindMiddleware = middleware => {
    if (process.env.NODE_ENV !== 'production') {
        middleware.push(logger);
        return composeWithDevTools(applyMiddleware(...middleware));
    }
    return applyMiddleware(...middleware);
};

const configureStore = (initialState = {}) => {
    const sagaMiddleware = createSagaMiddleware();
    const store = createStore(
        reducers,
        initialState,
        bindMiddleware([thunkMiddleware, sagaMiddleware])
    );

    store.sagaTask = sagaMiddleware.run(rootSaga);
    return store;
};

export default configureStore;
 */
